<?php
/**
 *
 * Author: Lyu
 * Date: 2023/7/25
 * Time: 19:50
 */

namespace App\Service\Core\Admin\User;

use App\Model\Core\Admin\AdminUserModel;
use App\Service\Core\CoreBaseService;
use App\Traits\ModelStoreTrait;

/**
 * @method $this setRole($val) role
 * @method $this setUsername($val) username
 * @method $this setPassword($val) password
 * @method $this setMobile($val) mobile
 * @method $this setStatus($val) status
 * @method $this setRemark($val) remark
 * @package App\Service\Core\Sys\User\User
 */
class AdminUserStoreService extends CoreBaseService
{
    use ModelStoreTrait;

    protected array $required_param = ['username', 'mobile', 'status'];

    /**
     * @param $user_id
     * @return array
     * @throws \Throwable
     */
    public function store($user_id = 0): array
    {
        $this->check_required_param($this->required_param);
        if (!empty($this->errors)) {
            return alert_info(1, $this->errors[0], $this->errors);
        }
        $username = $this->getParam('username');
        $password = $this->getParam('password');
        $mobile = $this->getParam('mobile');
        $role = $this->getParam('role');
        $status = $this->getParam('status');
        $remark = $this->getParam('remark');
//        $check = AdminUserModel::getInstance()->getUserByUsername($username);
//        if (!empty($check) && $user_id != $check['user_id']) {
//            return alert_info(1, '名称重复');
//        }
        $check = AdminUserModel::getInstance()->getUserByMobile($mobile);
        if (!empty($check) && $user_id != $check['user_id']) {
            return alert_info(1, '手机号已存在');
        }
        $data = [
            'role_id' => implode(',', $role),
            'username' => $username,
            'mobile' => $mobile,
            'status' => $status,
            'remark' => $remark
        ];
        $connection = AdminUserModel::getInstance()->getConnection();
        $connection->beginTransaction();
        if ($user_id) {
            $user_info = AdminUserModel::getInstance()->getOne($user_id);
            if (empty($user_info)) {
                $connection->rollBack();
                return alert_info(1, '编辑账户不存在');
            }
            $change_data = easy_check_change_data($user_info, $data);
            if (empty($change_data)) {
                $connection->commit();
                return alert_info(0, '操作成功', ['user_id' => $user_id]);
            }
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = AdminUserModel::getInstance()->updateOne($user_id, $data);
            $this->addLog('员工管理', '修改员工【' . $user_info['user_id'] . '】', $this->operate_id, $this->operate_by);
        } else {
            if (empty($password)) $password = config('sys.password_default');
            $data['password'] = bcrypt($password);
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['created_by'] = $this->operate_by;
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = AdminUserModel::getInstance()->addOne($data);
            $this->addLog('员工管理', '添加员工【' . $res . '】', $this->operate_id, $this->operate_by);
            $user_id = $res;
        }
        if ($res === false) {
            $connection->rollBack();
            return alert_info(1, '操作失败:数据库繁忙');
        }
        $connection->commit();
        return alert_info(0, '操作成功', ['user_id' => $user_id]);
    }
}
